#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Description: 基础功能测试相关
import os
import signal
import sys

import constants
from modules.check import Check
from modules.qaction import QAction
from modules.qanno import replicas_single, replicas_double
from modules.qbench import QBench
from modules.qdisk import get_disk_list, set_disk_status
from utils.logger import Logger

log = Logger()


class QDFSBasic:
    def __init__(self, parse_args):
        """
        传入指令值
        :param parse_args:
        """
        self.mode = parse_args.mode
        self.point = parse_args.point
        self.replicas = parse_args.replicas
        self.failgroup = parse_args.failgroup
        self.action = QAction(parse_args)
        self.bench = QBench(parse_args)
        self.check = Check(parse_args)

    @replicas_single(order=1, description="单副本创建QDFS文件系统", scene="basic")
    @replicas_double(order=1, description="双副本创建QDFS文件系统", scene="basic")
    def test_create_qdfs(self):
        """
        1.1测试用例
        :return:
        """
        # 这里获取到所有可以使用的盘，但是为了接下来的扩容测试，这里需要剔除最后一块盘
        qdisk_list = get_disk_list("UNUSED")[:-1]
        c_disks = ' '.join(qdisk_list)
        self.action.action_create_qdfs_no_failgroup(c_disks)
        m_disks = ':'.join(qdisk_list)
        self.action.action_mount_qdfs(m_disks)
        # 这些创建文件系统的盘，都需要标记未已经使用
        set_disk_status(qdisk_list, status="USED")
        self.check.qdfs_parameter_check()

    @replicas_single(order=2, description="单副本管理QDFS文件系统", scene="basic")
    def test_dev_s_qdfs(self):
        """
        1.2测试用例
        :return:
        """
        # 因为get_disk_list返回列表，所以这里只需要拿出一块盘进行扩容测试即可
        qdisk = get_disk_list(status="UNUSED")[0]
        self.action.action_dev_add_qdfs(qdisk)
        # 到这里，环境中所有的盘全部被使用完
        set_disk_status([qdisk], status="USED")
        used_disk = get_disk_list(status="USED")
        log.info(f"当前环境已经使用的盘数为{used_disk}")
        self.check.qdfs_parameter_check()

    @replicas_double(order=2, description="双副本管理QDFS文件系统", scene="basic")
    def test_dev_d_qdfs(self):
        qdisk = get_disk_list(status="UNUSED")[0]
        self.action.action_dev_add_qdfs(qdisk)
        self.action.action_dev_remove_qdfs(qdisk)
        self.action.action_dev_offline_qdfs(qdisk)
        self.action.action_dev_online_qdfs(qdisk)
        # 到这里，环境中所有的盘全部被使用完
        set_disk_status([qdisk], status="USED")
        used_disk = get_disk_list(status="USED")
        log.info(f"当前环境已经使用的盘数为{used_disk}")
        self.check.qdfs_parameter_check()

    @replicas_single(order=3, description="单副本块平衡测试", scene="basic")
    @replicas_double(order=3, description="双副本块平衡测试", scene="basic")
    def test_rereplicate_qdfs(self):
        """
        1.4测试用例
        :return:
        """
        self.action.action_data_rereplicate_qdfs()
        self.action.action_data_rereplicate_qdfs()

    @replicas_single(order=4, description="单副本大文件和小文件测试", scene="basic")
    @replicas_double(order=4, description="双副本大文件和小文件测试", scene="basic")
    def test_file_qdfs(self):
        """
        1.5测试用例
        :return:
        """
        try:
            size = self.bench.bench_fio_big_file(percent=1)
            if not self.check.check_file_size(size):
                os.kill(os.getpid(), signal.SIGTERM)
            else:
                log.info("big file create is success")

            self.bench.bench_vdbench_small_file()
            self.check.check_vdgfile()
        except Exception as e:
            log.error(f"error as {e}")
            sys.exit(1)

    @replicas_single(order=5, description="单副本文件及目录增删改查", scene="basic")
    @replicas_double(order=5, description="双副本文件及目录增删改查", scene="basic")
    def test_group_owner_qdfs(self):
        """
        1.6测试用例
        :return:
        """
        # 文件修改属组
        self.action.action_chown()
        self.check.check_qdfs_chown()
        # 文件修改权限
        self.action.action_chmod()
        self.check.check_qdfs_chmod()
        # 删除所有文件
        self.action.action_rm_rf()
        self.check.check_qdfs_space()

    @replicas_single(order=6, description="单副本fallocate测试", scene="basic")
    @replicas_double(order=6, description="双副本fallocate测试", scene="basic")
    def test_fallocate_qdfs(self):
        """
        1.7测试用例
        :return:
        """
        try:
            size = self.bench.bench_fallocate_file(10)
            if not self.check.check_file_size(size):
                os.kill(os.getpid(), signal.SIGTERM)

            log.success("【fallocate创建文件测试成功】")
        except Exception as e:
            log.error(f"error as {e}")
            sys.exit(1)

    @replicas_single(order=7, description="单副本副本数验证", scene="basic")
    @replicas_double(order=7, description="双副本副本数验证", scene="basic")
    def test_vertify_replicas_qdfs(self):
        """
        1.8测试用例
        :return:
        """
        try:
            self.check.check_replicas_disk(number=self.replicas)
            self.action.action_umount_qdfs()
            log.success(f"{self.point} is umount")
            # 这里将磁盘都清理掉
            used_disk = get_disk_list(status="USED")
            set_disk_status(qdisk_list=used_disk, status="UNUSED")
            log.success(f"{constants.local_data.disk_info} is clean")
        except Exception as e:
            log.error(f"error as {e}")
            sys.exit(1)